package com.itsm.xkitsm.liferoot.life.weather;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.itsm.xkitsm.liferoot.R;

/**
 * ���ݿ⴦����
 * 
 * 
 * 
 * @author smalt ����
 * 
 **/
public class DBUtils {
	private static SQLiteDatabase db;

	private final static String DATABASE_PATH = android.os.Environment
			.getExternalStorageDirectory().getAbsolutePath() + "/database";
	private static String DATABASE_FILENAME = "chinacity.db";

	// ʹ���ⲿ���ݿ�
	public static SQLiteDatabase openDatabase(Context context) {
		try {
			// Context context=new TestActivity();
			String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
			File dir = new File(DATABASE_PATH);
			if (!dir.exists()) // ����ļ��в����ڴ����ļ���
			{
				dir.mkdir();
				System.out.println("File build success");
			}
			if (!(new File(databaseFilename)).exists()) { // ����ļ������ڴ����ļ�
				InputStream is = context.getResources().openRawResource(
						R.raw.chinacity);
				FileOutputStream fos = new FileOutputStream(databaseFilename);
				byte[] buffer = new byte[8192];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}
				fos.close();
				is.close();
			}
			db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
		if (db != null) {
			System.out.println("db build success!");
		} else {
			System.out.println("db build failed!");
		}
		return db;
	}

}
